package com.miku.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

import static com.miku.constant.MessageConstant.*;

@Configuration
public class RabbitMqConfig {


    //声明生成普通交换机
    @Bean("BiExchange")
    public DirectExchange Biexchange(){
        // 三个参数：交换机名称、是否持久化、当没有queue与其绑定时是否自动删除
        return new DirectExchange(BI_EXCHANGE, true, false);
    }


    /**
     * 声明死信的交换机
     * @return
     */
    @Bean("deadLetterExchange")
    public DirectExchange deadLetterExchange(){
                return new DirectExchange(DEAD_LETTER_EXCHANGE);
    }
    //声明队列
    @Bean("BiChartQueue")
    public Queue BiQueue(){
        //使用QueueBulider构建队列,durable就是持久化的
        Map<String,Object> map=new HashMap<>(3);
        map.put("x-message-ttl",20000);
        map.put("x-dead-letter-exchange",DEAD_LETTER_EXCHANGE);
        map.put("x-dead-letter-routing-key",DEAD_LETTER_ROUTING_KEY);
        return QueueBuilder.durable(BI_QUEUE).withArguments(map).build();
    }

    /**
     * 生成文本转md的队列
     * @return
     */
    @Bean("BiToMdQueue")
    public Queue BiToMdQUeue(){
        //使用QueueBulider构建队列,durable就是持久化的
        Map<String,Object> map=new HashMap<>(3);
        map.put("x-message-ttl",20000);
        map.put("x-dead-letter-exchange",DEAD_LETTER_EXCHANGE);
        map.put("x-dead-letter-routing-key",DEAD_LETTER_ROUTING_KEY);
        return QueueBuilder.durable(BI_TO_MD_QUEUE).withArguments(map).build();
    }

    /**
     * 声明死信的队列
     * @return
     */
    @Bean("deadLetterQueue")
    public Queue  deadLetterQueue(){
        return QueueBuilder.durable(DEAD_LETTER_QUEUE).build();
    }

    /**
     * 绑定生成图表交换机和队列
     * @return
     */
    @Bean
    public Binding binding(@Qualifier("BiChartQueue") Queue queue,
                           @Qualifier("BiExchange") DirectExchange exchange){
       return BindingBuilder.bind(queue).to(exchange).with(BI_ROUTING_KEY);
    }

    /**
     * 绑定文本转md的交换机和队列
     * @return
     */
    @Bean
    public Binding biToMdBinding(@Qualifier("BiToMdQueue") Queue queue,
                                 @Qualifier("BiExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(BI_TO_MD_ROUTING_KEY);
    }


    /**
     * 将死信队列和死信交换机绑定
     * @param deadLetterQueueu
     * @param deadLetterExchage
     * @return
     */
    @Bean
    public Binding deadQueueBingingDeadExchange(@Qualifier("deadLetterQueue") Queue deadLetterQueueu,
                                                @Qualifier("deadLetterExchange") DirectExchange deadLetterExchage){
        return BindingBuilder.bind(deadLetterQueueu).to(deadLetterExchage).with(DEAD_LETTER_ROUTING_KEY);
    }


    @Bean
    public MessageRecoverer republishMessageRecoverer(RabbitTemplate rabbitTemplate){
        return new RepublishMessageRecoverer(rabbitTemplate, "error.direct", "error");
    }
}
